package com.linkoog.devtools.code_generator.template.model;

import com.linkoog.devtools.code_generator.template.model.node.FileNodeData;
import com.linkoog.devtools.code_generator.template.model.node.DirNodeData;
import com.linkoog.devtools.code_generator.template.model.node.TemplateNodeData;
import com.intellij.util.xmlb.annotations.XCollection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


public abstract class ContainerNodeData extends NodeData {


    @XCollection(elementTypes = { TemplateNodeData.class, DirNodeData.class, FileNodeData.class})
    private List<NodeData> childNodeList = new ArrayList<>();


    public List<NodeData> copyList(){
        List<NodeData> list = new ArrayList<>();
        for (NodeData nodeData : childNodeList){
            list.add(nodeData.deepCopy());
        }
        return list;
    }


    public void addChildNode(NodeData childNode){
        if (childNode != null){
            childNodeList.add(childNode);
        }
    }

    public void addChildNode(NodeData... childNodes){
        if (childNodes != null && childNodes.length > 0){
            for (NodeData childNode : childNodes){
                addChildNode(childNode);
            }
        }
    }

    public void addChildNode(Collection<NodeData> childNodes){
        if (childNodes != null && childNodes.size() > 0){
            for (NodeData childNode : childNodes){
                addChildNode(childNode);
            }
        }
    }

    public void addChildNode(int index, NodeData childNode){
        if (childNodeList.size() == 0 && index == 0){
            addChildNode(childNode);
        } else if ( index >= 0 && index < childNodeList.size()){
            if (childNode != null){
                childNodeList.add(index, childNode);
            }
        }else {
            addChildNode(childNode);
        }
    }

    public void removeChildNode(NodeData childNode){
        childNodeList.remove(childNode);
    }

    public void removeChildNode(int childNode){
        childNodeList.remove(childNode);
    }


    public List<NodeData> getChildNodeList() {
        return childNodeList;
    }

    public void setChildNodeList(List<NodeData> childNodeList) {
        this.childNodeList = childNodeList;
    }
}
